const app = getApp();
let QQMapWX = require('../../lib/qqmap-wx-jssdk.js');
var qqmapsdk;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showBg: false,
    name: '',
    // dname:'',
    currentIndex: 0,
    imglist: ['https://img1.baidu.com/it/u=2066520837,2885664453&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=310', 'https://img2.baidu.com/it/u=3165851643,2116206244&fm=253&fmt=auto&app=138&f=JPEG?w=781&h=500', 'https://img2.baidu.com/it/u=1387834533,3161760156&fm=253&fmt=auto&app=138&f=JPG?w=667&h=381','https://p0.meituan.net/dpmerchantpic/bbbfab5eb54cff7904030bd6b6788d0e_1_b8eb8f1aa8e3ef7a_1_WO13d0FhPjBxuG1YNscbLO%2F%2BiAm%2B42QMM2JVLaV5Bvy3sLY0jIWgu6abXE9uv%2FKw%2BTF4P5bclA%2BSRYaDxAbwY%2FSyrSVpT%2FmB25plNBxDGZzcN%2F1J5ULxCD%2BCCkbhFrJA']
  },
  // 获取搜索框 输入的内容
  getname: function (e) {
    this.data.name = e.detail.value;
    // console.log('输入内容',this.data.name)
  },
  delName:function(){
    this.data.name = ""
  },
  // 调用搜索 按钮
  selectName: function (e) {
    var that = this;
    // console.log("获取搜索框的值name:" + that.data.dname)
    console.log("获取搜索框的值name:" + that.data.name)
    if (that.data.name == '') {
      that.onLoad();
    } else {
      wx.request({
        url: app.globalData.headurl + '/Lfdshop/selectLfdshopByName',
        data: {
          name: that.data.name
          // dname: that.data.dname
        },
        method: "POST",
        header: {
          "content-type": "application/x-www-form-urlencoded;charset=utf-8",
          //token: app.globalData.token
        },
        success: function (r) {
          // console.log("获取列表成功")
          that.setData({
            lfdshoplist: r.data
          })
        }
      })

    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    var that = this;
    that.getLocation();
    wx.request({
      url: app.globalData.headurl + '/Lfdshop/selectLfdshop',
      method: "POST",
      header: {
        "content-type": "application/x-www-form-urlencoded;charset=utf-8",
        //token: app.globalData.token
      },
      success: function (r) {
        that.setData({
          lfdshoplist: r.data  //从服务器获取所有美发店的信息
        })
      }
    })


  },

  handleChange: function (e) {
    this.setData({
      currentIndex: e.detail.current
    })
  },

  
  findXy() { //获取用户的经纬度
    var _this = this
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        console.log("lat:" + res.latitude + "lng:" + res.longitude)
        _this.getDistance(res.latitude, res.longitude, 39.924091, 116.403414)
      }
    })
  },

  Rad: function (d) { //根据经纬度判断距离
    return d * Math.PI / 180.0;
  },
  getDistance: function (lat1, lng1, lat2, lng2) {
    // lat1用户的纬度
    // lng1用户的经度
    // lat2商家的纬度
    // lng2商家的经度
    var radLat1 = this.Rad(lat1);
    var radLat2 = this.Rad(lat2);
    var a = radLat1 - radLat2;
    var b = this.Rad(lng1) - this.Rad(lng2);
    var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
    s = s * 6378.137;
    s = Math.round(s * 10000) / 10000;
    s = s.toFixed(2) + '公里' //保留两位小数
    console.log('经纬度计算的距离:' + s)
    return s
  },

  compare: function (property, bol) {
    return function (a, b) {
      var value1 = a[property];
      var value2 = b[property];
      if (bol) {
        return value1 - value2;
      } else {
        return value2 - value1;
      }
    }
  },
  

  getLocation: function () {
    var that = this
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        that.getCity(res.latitude, res.longitude);
      }
    })
  },

  getCity: function (latitude, longitude) {
    var that = this;
    console.log("lat999:" + latitude + " lng:" + longitude)
    var url = "https://api.map.baidu.com/reverse_geocoding/v3/";
    var params = {
      ak: "p216eDob1pGzTFpzf5k4xYjX6EyjuiKA",
      output: "json",
      location: latitude + "," + longitude
    }
    console.log("params:" + JSON.stringify(params))
    wx.request({
      url: url,
      data: params,
      success: function (res) {
        var city = res.data.result.addressComponent.city;
        that.setData({
          city: city
        })

      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },


  todetail: function (e) {
    var id = e.currentTarget.dataset.id;
    var dname = e.currentTarget.dataset.dname;
    var img = e.currentTarget.dataset.img;
    wx.navigateTo({
      url: '../appointments/appointments?id=' + id + '&dname=' + dname,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //事件处理函数
  btnClickGoods: function () {
    wx.navigateTo({
      url: '../goods/goods?mark=home'
    })
  },

  btnClickAppointment: function () {
    wx.navigateTo({
      url: '../edit_appointment/edit_appointment'
    })
  },

  btnClickAppointments: function () {
    wx.navigateTo({
      url: '../appointments/appointments'
    })
  },
})